home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / CHESS.C < prev    next >
C/C++ Source or Header  |  1994-03-16  |  21KB  |  715 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is adapted from GNU Chess.
  12.     
  13.   C source for GNU CHESS
  14.  
  15.   Revision: 1990-09-30
  16.   Modified by Daryl Baker for use in MS WINDOWS environment
  17.  
  18.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  19.   Copyright (c) 1988, 1989, 1990  John Stanback
  20.  
  21.   This file is part of CHESS.
  22.  
  23.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  24.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  25.   the consequences of using it or for whether it serves any particular
  26.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  27.   General Public License for full details.
  28.  
  29.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  30.   only under the conditions described in the CHESS General Public License.
  31.   A copy of this license is supposed to have been given to you along with
  32.   CHESS so you can know your rights and responsibilities.  It should be in a
  33.   file named COPYING.  Among other things, the copyright notice and this
  34.   notice must be preserved on all copies.
  35. */
  36.  
  37. #define NOATOM                /*Minimize windows.h processing*/
  38. #define NOCLIPBOARD
  39. #define NOCREATESTRUCT
  40. #define NOFONT
  41. #define NOREGION
  42. #define NOSOUND
  43. #define NOWH
  44. #define NOKANJI
  45.  
  46. #define STRICT 
  47. #include <windows.h>         
  48. #include <windowsx.h>
  49. #include <commdlg.h>
  50. #include <string.h>
  51. #include <time.h>
  52. #include <ddeml.h>
  53.  
  54. #include "winsock.h"
  55. #include "gnuchess.h"
  56. #include "defs.h"
  57. #include "chess.h"
  58. #include "saveopen.h"
  59. #include "color.h"
  60. #include "resource.h"
  61.  
  62. // Message Cracker for socket message
  63. // void OnWsConnect(HWND hwnd, SOCKET socket, WORD wEvent, WORD wError)
  64. #define HANDLE_WM_SOCKET(hwnd, wParam, lParam, fn) \
  65.     (fn)((hwnd), (SOCKET)(wParam), (WORD)(WSAGETSELECTEVENT(lParam)), \
  66.                    (WORD)(WSAGETSELECTERROR(lParam)))
  67.                    
  68. DWORD clrBackGround;       /* rgb structures for various colors */
  69. DWORD clrBlackSquare;
  70. DWORD clrWhiteSquare;
  71. DWORD clrBlackPiece;
  72. DWORD clrWhitePiece;
  73. DWORD clrText;
  74.  
  75. static HBRUSH hBrushBackGround;
  76.  
  77. short boarddraw[64];       /* Display copies of the board */
  78. short colordraw[64];       /* Needed because while computer is calculating*/
  79.                            /* moves it updates board and color thus you can*/
  80.                            /* not repaint the screen accuratly */
  81.  
  82. struct PIECEBITMAP pieces[7];
  83.  
  84. HINSTANCE hInst;              /* current instance */
  85.  
  86. char szAppName[] = "WSChess";
  87.  
  88. extern char mvstr[4][6];
  89. int coords = 1;
  90.  
  91. static int FirstSq = -1;         /* Flag is a square is selected */
  92. static int GotFirst = FALSE;
  93. static int EditActive = FALSE;   
  94. int User_Move = TRUE;    
  95. int xchar, ychar;
  96. static HMENU hMainMenu;
  97.  
  98. HWND hwndHostDlg=NULL;
  99. DWORD idDdeServInst,idDdeClntInst;
  100. WORD wComType=IDC_SOCKETS,wPacketSize;
  101. BOOL bConnected=FALSE,bHost=FALSE,bMoveReady=FALSE,bWaiting=FALSE;
  102. BOOL (*Initialise)(void);
  103. BOOL (*ConnectHost)(void);
  104. BOOL (*ConnectClient)(void);
  105. void (*GetOpponentsMove)(WORD);
  106. void (*SendMove)(WORD);
  107. BOOL (*IsCommand)(LPSTR);
  108. void (*LoadGame)(void);
  109. void (*StartGame)(void);
  110. void (*SetTime)(int);
  111. HSZ hszServName,hszTopic,hszServNameCl,hszTopicCl,hszItem,hszGet;
  112. HCONV hconvHost,hconvClient;
  113. HWND hwndMain,hwndStatus;
  114. MoveInfo moveinfo;
  115. char szSockDesc[45];
  116.  
  117. void OnSocketMessage(HWND , SOCKET , WORD , WORD );
  118.  
  119. void EnableMenuItems(void)
  120. //------------------------
  121. { HMENU hmenuMain;
  122.   
  123.   hmenuMain = GetMenu(hwndMain);
  124.   if ( (bConnected) && (User_Move) )
  125.    { EnableMenuItem(hmenuMain,MSG_CHESS_NEW,MF_ENABLED | MF_BYCOMMAND);
  126.      EnableMenuItem(hmenuMain,MSG_CHESS_EDIT,MF_ENABLED | MF_BYCOMMAND);
  127.      EnableMenuItem(hmenuMain,MSG_CHESS_GET,MF_ENABLED | MF_BYCOMMAND);
  128.      EnableMenuItem(hmenuMain,IDM_TIMECONTROL,MF_ENABLED | MF_BYCOMMAND);
  129.    }  
  130.   else   
  131.    { EnableMenuItem(hmenuMain,MSG_CHESS_NEW,MF_GRAYED | MF_BYCOMMAND);
  132.      EnableMenuItem(hmenuMain,MSG_CHESS_EDIT,MF_GRAYED | MF_BYCOMMAND);
  133.      EnableMenuItem(hmenuMain,MSG_CHESS_GET,MF_GRAYED | MF_BYCOMMAND);
  134.      EnableMenuItem(hmenuMain,IDM_TIMECONTROL,MF_GRAYED | MF_BYCOMMAND);
  135.    }
  136.   if (bConnected)
  137.    { EnableMenuItem(hmenuMain,IDM_DISCONNECT,MF_ENABLED | MF_BYCOMMAND);  
  138.      EnableMenuItem(hmenuMain,IDM_PROTOCOL,MF_GRAYED | MF_BYCOMMAND);  
  139.      EnableMenuItem(hmenuMain,IDM_HOST,MF_GRAYED | MF_BYCOMMAND);  
  140.      EnableMenuItem(hmenuMain,IDM_CLIENT,MF_GRAYED | MF_BYCOMMAND);  
  141.    }
  142.   else
  143.    { EnableMenuItem(hmenuMain,IDM_DISCONNECT,MF_GRAYED | MF_BYCOMMAND);  
  144.      EnableMenuItem(hmenuMain,IDM_PROTOCOL,MF_ENABLED | MF_BYCOMMAND);  
  145.      EnableMenuItem(hmenuMain,IDM_HOST,MF_ENABLED | MF_BYCOMMAND);  
  146.      EnableMenuItem(hmenuMain,IDM_CLIENT,MF_ENABLED | MF_BYCOMMAND);  
  147.    }  
  148. }   
  149.  
  150. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  151.                   LPSTR lpCmdLine,int nCmdShow)
  152. //--------------------------------------------------------------                  
  153. {
  154.     HWND hWnd;
  155.     MSG msg;
  156.     POINT pt;
  157.  
  158.     lpCmdLine ++;
  159.  
  160.     if (!hPrevInstance)
  161.        if (!ChessInit(hInstance)) return (NULL);
  162.  
  163.     hInst = hInstance;          /* Saves the current instance        */
  164.  
  165.     QueryBoardSize (&pt);
  166.  
  167.     /* Create the main window.  It will be autosized in WM_CREATE message */
  168.     hWnd = CreateWindow(szAppName,  szAppName,
  169.         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  170.          CW_USEDEFAULT, CW_USEDEFAULT,
  171.          CW_USEDEFAULT, CW_USEDEFAULT,
  172.          NULL,  NULL,   hInstance,  NULL);
  173.  
  174.     if (!hWnd)  return (NULL);
  175.     hwndMain = hWnd;
  176.     ShowWindow(hWnd, nCmdShow);          
  177.  
  178.    /* Initialize chess */
  179.    if (init_main ( hWnd ) ) {
  180.       SMessageBox (hWnd, IDS_INITERROR,IDS_CHESS);
  181.       FreeGlobals ();
  182.       return (NULL);
  183.    }
  184.  
  185.    UpdateWindow(hWnd);          
  186.    EnableMenuItems();
  187.  
  188.    player = opponent;
  189.  
  190.    while (GetMessage(&msg, NULL, NULL, NULL)) {
  191.        TranslateMessage(&msg);     
  192.        DispatchMessage(&msg);
  193.     }
  194.  
  195.     return (msg.wParam);   
  196. }
  197.  
  198.  
  199. void CreateStatusBar(HWND hWnd,HINSTANCE hInst,short ychar)
  200. //---------------------------------------------------------
  201. { RECT rectWinRect;
  202.   int ny;
  203.   
  204.   GetClientRect(hWnd, &rectWinRect);                      
  205.   ny = rectWinRect.bottom - (15 + ychar);
  206.   hwndStatus = CreateWindow("STATBR",NULL,WS_CHILD | WS_VISIBLE,
  207.                             0,ny,rectWinRect.right,rectWinRect.bottom - ny,
  208.                             hWnd,(HMENU)5000,hInst,NULL);
  209.   ShowWindow(hwndStatus,SW_SHOW);                          
  210. }
  211.   
  212. // Handle WM_CREATE message
  213. BOOL OnCreate(HWND hWnd,CREATESTRUCT FAR *lpCreateStruct)
  214. //-------------------------------------------------------
  215. { int i; 
  216.   HDC hDC;
  217.   TEXTMETRIC tm;
  218.   POINT point;
  219.  
  220.   hwndMain = hWnd;
  221.   GetStartupColors();
  222.   hBrushBackGround = CreateSolidBrush ( clrBackGround );
  223.  
  224.   for ( i=pawn; i<pawn+6; i++ ) 
  225.     pieces[i].piece = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+i));
  226.   for ( i=pawn; i<pawn+6; i++ ) 
  227.     pieces[i].mask = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+6+i));
  228.   for ( i=pawn; i<pawn+6; i++ ) 
  229.     pieces[i].outline = LoadBitmap (hInst, MAKEINTRESOURCE(PAWNBASE+12+i));
  230.  
  231.   hDC = GetDC (hWnd);
  232.   GetTextMetrics ( hDC, &tm);
  233.   xchar = tm.tmMaxCharWidth;
  234.   ychar = tm.tmHeight+tm.tmExternalLeading;
  235.  
  236.   /*Autosize main window */
  237.   QueryBoardSize (&point);            
  238.   SetWindowPos( hWnd, hWnd, 0,0,
  239.                 point.x+GetSystemMetrics(SM_CXFRAME)*2+50,
  240.                 point.y+GetSystemMetrics(SM_CYFRAME)*2+GetSystemMetrics(SM_CYMENU)+
  241.                 GetSystemMetrics(SM_CYCAPTION) + 2*ychar,
  242.                 SWP_NOMOVE | SWP_NOZORDER);
  243.  
  244.   ReleaseDC ( hWnd, hDC);
  245.  
  246.   InitHitTest ();
  247.   hComputerMove = CreateWindow ("Static",NULL,
  248.                         WS_CHILD | SS_LEFT | WS_VISIBLE,
  249.                         375,10 ,10*xchar,ychar,
  250.                         hWnd, (HMENU)1003, hInst, NULL);
  251.   CreateStatusBar(hWnd,hInst,ychar);
  252.   SetWindowText(hWnd,"Winsock Chess (Not Connected)");
  253.   GetOpponentsMove = GetOpponentsMoveWS;
  254.   SendMove         = SendMoveWS;
  255.   LoadGame         = GetGameWS;
  256.   IsCommand        = IsCommandWS;
  257.   StartGame        = NewGameWS;
  258.   SetTime          = SetTimeWS;
  259.   wPacketSize = 0;
  260.   return TRUE;
  261. }
  262.  
  263. void FreeBitmaps(void)
  264. //--------------------
  265. { register int i;
  266.  
  267.   for ( i=pawn; i<pawn+6; i++ ) 
  268.     DeleteObject(pieces[i].piece);
  269.   for ( i=pawn; i<pawn+6; i++ ) 
  270.     DeleteObject(pieces[i].mask);
  271.   for ( i=pawn; i<pawn+6; i++ ) 
  272.     DeleteObject(pieces[i].outline);
  273. }    
  274.  
  275. void FreeWindows(void)
  276. //--------------------
  277. {  
  278.   DestroyWindow(hComputerMove);
  279.   DestroyWindow(hwndStatus);
  280. }  
  281.  
  282. void Disconnect(void)
  283. //-------------------
  284. { if (! bConnected) return;
  285.   switch (wComType)
  286.    { case IDC_DDE :
  287.         DisconnectDDE();
  288.         NewGame(hwndMain);  
  289.         break;
  290.      case IDC_SOCKETS :
  291.         DisconnectWS();
  292.         NewGame(hwndMain);  
  293.         break;    
  294.    }      
  295.   bConnected = FALSE; 
  296. }
  297.         
  298. // Handle WM_DESTROY message
  299. BOOL OnDestroy(HWND hWnd)
  300. //-----------------------
  301. { Disconnect();
  302.   DeleteObject (hBrushBackGround);
  303.   Hittest_Destructor ();
  304.   FreeGlobals ();
  305.   FreeBitmaps();
  306.   FreeWindows();
  307.   SaveColors();
  308.   PostQuitMessage(0);
  309.   return TRUE;
  310. }  
  311.  
  312. //Handle WM_PAINT message
  313. BOOL OnPaint(HWND hWnd)
  314. //---------------------
  315. { POINT pt; 
  316.   RECT rect;  
  317.   HDC hDC;
  318.   PAINTSTRUCT ps;
  319.   
  320.   if ( FirstSq != -1 )         /*Properly repaint hilighted square*/
  321.    { QuerySqOrigin ( FirstSq%8, FirstSq/8, &pt);
  322.      rect.left = pt.x; rect.right=pt.x+48;
  323.      rect.top = pt.y-48; rect.bottom = pt.y;
  324.      InvalidateRect (hWnd, &rect, FALSE);
  325.    }
  326.  
  327.   hDC = BeginPaint ( hWnd, &ps);
  328.   Draw_Board (hDC, flag.reverse, clrBlackSquare, clrWhiteSquare );
  329.   if ( coords ) 
  330.     DrawCoords (hDC, flag.reverse, clrBackGround, clrText);
  331.   DrawAllPieces (hDC, flag.reverse, boarddraw, colordraw, 
  332.                  clrBlackPiece, clrWhitePiece );
  333.   RedrawStatusBar();
  334.   EndPaint ( hWnd, &ps);
  335.  
  336.   if ( FirstSq != -1 ) 
  337.      HiliteSquare ( hWnd, FirstSq);
  338.   return TRUE;
  339. }  
  340.  
  341. //Handle WM_CTLCOLOR message
  342. HBRUSH OnCtlColor(HWND hWnd, HDC hdc, HWND hwndChild, int type)
  343. //-------------------------------------------------------------
  344. { POINT point;
  345.   
  346.   if ( type == CTLCOLOR_STATIC) 
  347.    { UnrealizeObject ( hBrushBackGround );
  348.      SetBkColor(hdc, clrBackGround);
  349.      SetBkMode(hdc, TRANSPARENT);
  350.      SetTextColor (hdc, clrText);
  351.      point.x = point.y = 0;
  352.      ClientToScreen ( hWnd, &point);
  353.      SetBrushOrg(hdc, point.x, point.y);
  354.      return hBrushBackGround;
  355.    } 
  356.   else
  357.      return NULL;
  358. }
  359.  
  360. // Handle WM_ERASEBKGND message
  361. BOOL OnEraseBkgnd(HWND hWnd, HDC hdc)
  362. //-----------------------------------
  363. { RECT rect;
  364.  
  365.   UnrealizeObject ( hBrushBackGround);
  366.   GetClientRect ( hWnd, &rect);
  367.   FillRect (hdc, &rect, hBrushBackGround);
  368.   return TRUE;
  369. }
  370.  
  371. // Handle WM_INITMENUPOPUP message
  372. void OnInitMenuPopup(HWND hWnd,HMENU hMenu, int item, BOOL bSystemMenu)
  373. //---------------------------------------------------------------------
  374. { if ( !EditActive ) 
  375.     Init_Menus (hWnd, hMenu, item);
  376. }
  377.  
  378. // Handle WM_LBUTTONDOWN message
  379. void OnLButtonDown(HWND hWnd,BOOL bDoubleClick,int x,int y,UINT keyFlags)
  380. //----------------------------------------------------------------------
  381. { int Hit;
  382.   
  383.   if (! User_Move) return;
  384.   if (! bConnected) return;
  385.   Hit = HitTest (x, y);
  386.  
  387.   if ( Hit == -1 )
  388.    { if ( FirstSq != -1) 
  389.       { UnHiliteSquare ( hWnd, FirstSq);
  390.         GotFirst = FALSE;
  391.         FirstSq = -1;
  392.       }
  393.      return;
  394.    }
  395.  
  396.   if ( GotFirst ) 
  397.    { UnHiliteSquare( hWnd, FirstSq);
  398.      GotFirst = FALSE;
  399.      if ( EditActive == TRUE) 
  400.        PostMessage ( hWnd, MSG_EDITBOARD,(WPARAM)FirstSq,(LPARAM)Hit);      
  401.      else 
  402.        if ( User_Move == TRUE) 
  403.          PostMessage ( hWnd, MSG_USER_ENTERED_MOVE, (WPARAM)FirstSq,
  404.                        (LPARAM)Hit),
  405.      FirstSq = -1;
  406.    } 
  407.   else 
  408.     { GotFirst = TRUE;
  409.       FirstSq = Hit;
  410.       HiliteSquare ( hWnd, Hit);
  411.     }
  412.   return;
  413. }
  414.  
  415. void OnCommand(HWND hWnd,int id,HWND hwndCtl,UINT wcodeNotify)
  416. //-----------------------------------------------------------
  417. { char szFileName[256];
  418.          
  419.   switch (id) 
  420.     { case MSG_CHESS_QUIT:
  421.         DestroyWindow ( hWnd);
  422.         break;
  423.  
  424.       case MSG_CHESS_NEW:
  425.         (*StartGame)();
  426.         break;
  427.  
  428.       case MSG_CHESS_ABOUT:
  429.         DialogBox(hInst,MAKEINTRESOURCE(AboutBox),hWnd,About);
  430.         break;
  431.         
  432.       case MSG_CHESS_REVIEW:
  433.         ReviewDialog ( hWnd, hInst );
  434.         break;
  435.  
  436.       case MSG_CHESS_REVERSE:
  437.         flag.reverse = !flag.reverse;
  438.         UpdateDisplay(hWnd,0,0,1,0);
  439.         break;
  440.       
  441.       case MSG_CHESS_SAVE :
  442.         if (GetFileName(szFileName,OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
  443.                         MSG_CHESS_SAVE))
  444.           SaveGame(hwndMain,szFileName);               
  445.         break;
  446.       
  447.       case MSG_CHESS_GET :
  448.         (*LoadGame)();
  449.         break;
  450.       
  451.       case MSG_CHESS_LIST :
  452.          if (GetFileName(szFileName,OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT,
  453.                         MSG_CHESS_LIST))
  454.             ListGame(hWnd,szFileName);
  455.          break; 
  456.       
  457.       case MSG_CHESS_HASH:
  458.         flag.hash = !flag.hash;
  459.          break;
  460.  
  461.       case MSG_CHESS_BEEP:
  462.         flag.beep = !flag.beep;
  463.         break;
  464.  
  465.       case MSG_CHESS_COORD:
  466.         coords = !coords;
  467.         UpdateDisplay(hWnd,0,0,1,0);
  468.         break;
  469.  
  470.       case MSG_CHESS_ABORT:
  471.         flag.timeout = true;
  472.         flag.bothsides = false;
  473.         EnableMenuItem ( GetMenu(hWnd), MENU_ID_ABORT, MF_GRAYED | MF_BYCOMMAND | MF_GRAYED | MF_BYCOMMAND | MF_BYPOSITION );
  474.         DrawMenuBar ( hWnd );
  475.         break;
  476.  
  477.       case IDM_BACKGROUND:
  478.         if (ColorDialog ( hWnd, id) ) 
  479.           { InvalidateRect (hWnd, NULL, TRUE);
  480.             DeleteObject (hBrushBackGround);
  481.             hBrushBackGround = CreateSolidBrush ( clrBackGround );
  482.             
  483.             InvalidateRect (hComputerMove, NULL, TRUE);
  484.           }
  485.         break;
  486.  
  487.       case IDM_BLACKSQUARE:
  488.         if (ColorDialog ( hWnd, id) ) 
  489.           InvalidateRect (hWnd, NULL, TRUE);
  490.         break;
  491.  
  492.       case IDM_WHITESQUARE:
  493.         if (ColorDialog ( hWnd, id) ) 
  494.            InvalidateRect (hWnd, NULL, TRUE);
  495.         break;
  496.         
  497.       case IDM_BLACKPIECE:
  498.         if (ColorDialog ( hWnd, id) ) 
  499.            InvalidateRect (hWnd, NULL, TRUE);
  500.         break;
  501.  
  502.       case IDM_WHITEPIECE:
  503.         if (ColorDialog ( hWnd, id) ) 
  504.            InvalidateRect (hWnd, NULL, TRUE);
  505.         break;
  506.  
  507.       case IDM_TEXT:
  508.         if ( ColorDialog (hWnd, id) ) 
  509.           { InvalidateRect (hWnd, NULL, TRUE);
  510.             InvalidateRect (hComputerMove, NULL, TRUE);
  511.           }
  512.         break;
  513.  
  514.       case IDM_DEFAULT:
  515.         SetStandardColors ();
  516.         InvalidateRect (hWnd, NULL, TRUE);
  517.         DeleteObject (hBrushBackGround);
  518.         hBrushBackGround = CreateSolidBrush ( clrBackGround );
  519.  
  520.         InvalidateRect (hComputerMove, NULL, TRUE);
  521.         RedrawStatusBar();
  522.         break;
  523.  
  524.       case IDM_TIMECONTROL:
  525.         (*SetTime)(id);    
  526.         RedrawStatusBar();
  527.         break;
  528.         
  529.       case IDM_PROTOCOL :
  530.         DialogBox(hInst,MAKEINTRESOURCE(IDD_PROTOCOL),hWnd,ProtocolDlg);
  531.         break;  
  532.       
  533.       case IDM_HOST :
  534.         switch (wComType)
  535.           { case IDC_DDE :
  536.               DialogBox(hInst,MAKEINTRESOURCE(IDD_HOSTDDE),hWnd,HostDlgDDE);
  537.               break;
  538.             case IDC_SOCKETS :
  539.               DialogBox(hInst,MAKEINTRESOURCE(IDD_HOSTWS),hWnd,HostDlgWS);
  540.               break;
  541.           }    
  542.         RedrawStatusBar();  
  543.         break;  
  544.         
  545.       case IDM_CLIENT :
  546.         switch (wComType)
  547.           { case IDC_DDE :
  548.               DialogBox(hInst,MAKEINTRESOURCE(IDD_CONNDDE),hWnd,ConnDdeDlg);
  549.               break;
  550.             case IDC_SOCKETS :
  551.               DialogBox(hInst,MAKEINTRESOURCE(IDD_CONNWS),hWnd,ConnWSDlg);
  552.               break;    
  553.           }    
  554.         RedrawStatusBar();  
  555.         break;  
  556.           
  557.       case IDM_DISCONNECT :
  558.         Disconnect();
  559.         RedrawStatusBar();
  560.         break;  
  561.         
  562.       case IDM_HELP :
  563.         WinHelp(hWnd,".\\WSCHESS.HLP",HELP_CONTENTS,0L);
  564.         break;
  565.     }
  566. }        
  567.  
  568. void swap(short *col1,short *col2)
  569. //------------------------------
  570. { short tmp;
  571.  
  572.   tmp = *col1;
  573.   *col1 = *col2;
  574.   *col2 = tmp;
  575. }
  576.   
  577. LRESULT CALLBACK ChessWndProc( HWND hWnd, UINT message, WPARAM wParam, 
  578.                                LPARAM lParam)
  579. //--------------------------------------------------------------------                               
  580. {  char str[80];
  581.    unsigned short mv;
  582.    int Square,First,algbr_flag,temp;
  583.  
  584.    switch (message) 
  585.    { case WM_CREATE: 
  586.           return HANDLE_WM_CREATE(hWnd,wParam,lParam,OnCreate);
  587.  
  588.      case WM_DESTROY:  
  589.           return HANDLE_WM_DESTROY(hWnd,wParam,lParam,OnDestroy);
  590.  
  591.       case WM_PAINT:
  592.           return HANDLE_WM_PAINT(hWnd,wParam,lParam,OnPaint);
  593.  
  594.       case WM_CTLCOLOR:  
  595.           return HANDLE_WM_CTLCOLOR(hWnd,wParam,lParam,OnCtlColor);
  596.  
  597.       case WM_ERASEBKGND:
  598.           return HANDLE_WM_ERASEBKGND(hWnd,wParam,lParam,OnEraseBkgnd);
  599.  
  600.       case WM_INITMENUPOPUP:
  601.           return HANDLE_WM_INITMENUPOPUP(hWnd,wParam,lParam,OnInitMenuPopup);         
  602.  
  603.       case WM_LBUTTONDOWN:
  604.           return HANDLE_WM_LBUTTONDOWN(hWnd,wParam,lParam,OnLButtonDown);
  605.       
  606.       case WM_COMMAND :
  607.           return HANDLE_WM_COMMAND(hWnd,wParam,lParam,OnCommand);
  608.       
  609.       case WM_SOCKET :
  610.           HANDLE_WM_SOCKET(hWnd,wParam,lParam,OnSocketMessage);
  611.           return 0;
  612.           
  613.       case MSG_EDITBOARD:
  614.       {
  615.          int Square, First;
  616.  
  617.          if ( flag.reverse ) {
  618.             First = 63 - wParam;
  619.             Square  = 63 - (int)lParam;
  620.          } else {
  621.             First = wParam;
  622.             Square  = (int)lParam;
  623.          }
  624.          
  625.          board[Square] = board[First];
  626.          color[Square] = color[First];
  627.  
  628.          board[First] = no_piece;
  629.          color[First] = neutral;
  630.  
  631.          UpdateDisplay (hWnd, First, Square, false, false);
  632.       }
  633.          break;
  634.  
  635.       case MSG_USER_MOVE:
  636.             RedrawStatusBar();
  637.             EnableMenuItems();
  638.             ft = 0;
  639.             player = opponent;
  640.          break;
  641.  
  642.       case MSG_USER_ENTERED_MOVE:
  643.       {  User_Move = FALSE;
  644.          player = opponent;
  645.  
  646.          First = wParam;
  647.          Square  = (int)lParam;
  648.          
  649.          if (flag.reverse)
  650.            { First = 63 - wParam;
  651.              Square  = 63 - (int)lParam;
  652.            } 
  653.          
  654.          /* Logic to allow selection for pawn promotion */
  655.          if ( (board[First] == pawn) &&( (Square <8) || (Square>55)) ) {
  656.             algbr_flag = promote + PromoteDialog (hWnd, hInst);
  657.          } else algbr_flag = 0;
  658.          algbr ( First, Square, algbr_flag);
  659.  
  660.          lstrcpy(str,mvstr[0]);
  661.          
  662.          temp = VerifyMove ( hWnd, str, 0, &mv);
  663.          
  664.          if ( temp == TRUE) 
  665.          {  ElapsedTime (1);     
  666.             strcpy(moveinfo.szMove,str); 
  667.             strcat(moveinfo.szMove,"\r\n");
  668.             (*SendMove)(7);
  669.             PostMessage ( hWnd, MSG_COMPUTER_MOVE, NULL, (LPARAM)NULL);
  670.          } 
  671.          else 
  672.          { User_Move = TRUE;
  673.            PostMessage ( hWnd, MSG_USER_MOVE, NULL, (LPARAM)NULL);
  674.          }  
  675.       }
  676.         break;
  677.  
  678.       case MSG_COMPUTER_MOVE:
  679.          EnableMenuItems();
  680.          RedrawStatusBar();
  681.          if ( !(flag.quit || flag.mate || flag.force) )
  682.           { User_Move = FALSE;            
  683.             if (! bWaiting)
  684.              (*GetOpponentsMove)(7);
  685.             else
  686.              { bWaiting = FALSE;  
  687.                swap(&opponent,&computer);
  688.                player = opponent;
  689.                strncpy(str,moveinfo.szMove,5); str[5] = 0; 
  690.                if (str[4] == '\r') str[4] = 0;
  691.                if (! ((*IsCommand)(str)))
  692.                  { temp = VerifyMove ( hWnd, str, 0, &mv);
  693.                    if ( flag.beep ) MessageBeep (0);
  694.                    swap(&opponent,&computer);
  695.                    User_Move = TRUE;
  696.                    OutputMove(hWnd);
  697.                    PostMessage (hWnd, MSG_USER_MOVE, NULL,NULL);
  698.                  } 
  699.                else
  700.                  { swap(&opponent,&computer);
  701.                    if (!User_Move)
  702.                      PostMessage ( hWnd, MSG_COMPUTER_MOVE, NULL, (LPARAM)NULL);
  703.                    else  
  704.                      PostMessage ( hWnd, MSG_USER_MOVE, NULL, (LPARAM)NULL);
  705.                  }  
  706.              }  
  707.           }         
  708.          break;
  709.  
  710.       default:   
  711.          return (DefWindowProc(hWnd, message, wParam, lParam));
  712.     }
  713.     return (NULL);
  714. }
  715.